package com.LeeCode;

import java.util.HashMap;

/**
 * 统计最大组的数目
 */

public class Code1399 {
    public static void main(String[] args) {
        System.out.println(new Code1399().countLargestGroup1(13));
    }

    public int countLargestGroup(int n) {
        int ans = 0, maxCount = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            String str = String.valueOf(i);
            int tmp = 0;
            for (char c : str.toCharArray()) {
                tmp += c - '0';
            }
            map.put(tmp, map.getOrDefault(tmp, 0) + 1);
            maxCount = Math.max(maxCount, map.get(tmp));
        }

        for (int key : map.keySet()) {
            if (map.get(key) == maxCount) {
                ans++;
            }
        }
        return ans;
    }

    public int countLargestGroup1(int n) {
        int ans = 0, maxCount = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            String str = String.valueOf(i);
            int tmp = 0, aux = i;
            while (aux != 0) {
                tmp += aux % 10;
                aux /= 10;
            }
            map.put(tmp, map.getOrDefault(tmp, 0) + 1);
            maxCount = Math.max(maxCount, map.get(tmp));
        }

        for (int key : map.keySet()) {
            if (map.get(key) == maxCount) {
                ans++;
            }
        }
        return ans;
    }
}
